package at.codebase.core;

import java.security.SecureRandom;
import java.util.ArrayList;

import org.apache.commons.codec.binary.Base64;

import at.codebase.core.search.SearchTerm;
import at.codebase.security.SecureStringBuilder;

/**
 * @author Daniel Marth <danielmarth@gmx.at>
 */
public class StringTools {

	public static final String Quotes = "\"\'";
	public static final String DefaultQueyQuote = "\"";
	public static final String QuerySeparators = " ";

	public static ArrayList<SearchTerm> buildItemSearchQuery(String line) {
		ArrayList<SearchTerm> terms = new ArrayList<SearchTerm>();
		StringBuilder current = new StringBuilder();
		boolean quote = false;
		for (int i = 0; i < line.length(); i++) {
			if (Quotes.contains(new String(new char[] { line.charAt(i) }))) {
				quote = !quote;
				if (!quote && !current.toString().trim().isEmpty()) {
					current.append(DefaultQueyQuote);
					terms.add(new SearchTerm(current.toString().trim()));
					current = new StringBuilder();
				} else {
					current.append(" ");
					current.append(DefaultQueyQuote);
				}
			} else if (!QuerySeparators.contains(new String(new char[] { line
					.charAt(i) })) || quote) {
				current.append(line.charAt(i));
			} else {
				if (current.toString().startsWith(DefaultQueyQuote)) {
					current.delete(0, 1);
				}
				if (!current.toString().trim().isEmpty()) {
					terms.add(new SearchTerm(current.toString().trim()));
					current = new StringBuilder();
				}
			}
		}
		if (quote) {
			current.append(DefaultQueyQuote);
		}
		if (!current.toString().trim().isEmpty()) {
			if (current.toString().startsWith(DefaultQueyQuote)) {
				current.delete(0, 1);
			}
			terms.add(new SearchTerm(current.toString().trim()));
		}
		return terms;
	}

	public static ArrayList<String> splitConsideringQuotes(String line,
			String separators) {
		ArrayList<String> splittedLine = new ArrayList<String>();
		StringBuilder current = new StringBuilder();
		boolean quote = false;
		for (int i = 0; i < line.length(); i++) {
			if (line.charAt(i) == '\"') {
				quote = !quote;
			} else if (!separators.contains(new String(new char[] { line
					.charAt(i) })) || quote) {
				current.append(line.charAt(i));
			} else {
				splittedLine.add(current.toString().trim());
				current.delete(0, current.length());
			}
		}
		if (!current.toString().trim().isEmpty()) {
			splittedLine.add(current.toString().trim());
		}
		return splittedLine;
	}

	public static String generateSubstrings(String str) {
		SecureStringBuilder builder = new SecureStringBuilder();
		for (int i = 1; i < str.length(); i++) {
			builder.appendInsecure(" ");
			builder.appendInsecure(str.substring(0, i));
		}
		return builder.toString().trim();
	}

	public static String generateBase64RandomString() {
		SecureRandom secureRandom = new SecureRandom();
		byte seed[] = secureRandom.generateSeed(32);
		StringBuilder key = new StringBuilder();
		for (int i = 0; i < seed.length; i++) {
			seed[i] = (byte) Math.abs(seed[i]);
			key.append(seed[i] % 64);
		}
		return new String(new Base64().encode(key.toString().getBytes()));
	}

}
